//
//  OuptPrescribeVC.m
//  掌上宏力
//
//  Created by admin on 2017/5/7.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "OuptPrescribeVC.h"
#import "OuptMainModel.h"
#import "OuptPrescribeModel.h"
#import "OuptPrescribeCell.h"
#import "User.h"
#import "OuptTopView.h"

NSString *const prescItem_url = @"api/Account/GetOuptPrescListByvisitNo";

@interface OuptPrescribeVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSArray *prescribesArray;

@property (nonatomic,strong) UITableView *tableView;
@end

@implementation OuptPrescribeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"就诊详情";
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [self getPrescribeDatas];
}

-(void)getPrescribeDatas{
    //{patient_id:"90000476",visit_no:"30367"}
    User *user = [User currentUser];
    NSDictionary *dic = @{@"patient_id":user.patientId,@"visit_no":_ouptModel.visitNo};
    NSString *url = [NSString stringWithFormat:@"%@%@",HOSPITAL_URL,prescItem_url];
    [NetWorker getCommonDatasWithURLStr:url body:dic success:^(id result) {
        NSArray *array = [JsonHelper arrayWithJsonString:result[@"Entity"]];
        NSMutableArray *prescArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            OuptPrescribeModel *model = [OuptPrescribeModel setPrescribeModel:dic];
            [prescArray addObject:model];
        }
        _prescribesArray = [NSArray arrayWithArray:prescArray];
        [SVProgressHUD dismiss];
        //创建视图
        [self createPrescribeView];
        [_tableView reloadData];
    } failure:^(id result) {
        [SVProgressHUD dismiss];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
-(void)createPrescribeView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    OuptTopView *topView = [[OuptTopView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 100)];
    topView.model = self.ouptModel;
    _tableView.tableHeaderView = topView;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _prescribesArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    OuptPrescribeModel *model = _prescribesArray[section];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 45)];
    view.backgroundColor = TB_COLOR;
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenW-20, 45)];
    titleLab.text = [NSString stringWithFormat:@"%@  (%@  %@)",model.drugName,model.amount,model.units];
    titleLab.font = kFont(16);
    [view addSubview:titleLab];
    return view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OuptPrescribeCell *cell = [OuptPrescribeCell cellWithTableView:tableView];
    cell.prescribeModel = _prescribesArray[indexPath.section];;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
